package com.drama.api.sys.video.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.json.JSONObject;
import com.drama.api.base.ApiIndex;
import com.drama.api.base.IDEntity;
import com.drama.api.base.R;
import com.drama.api.base.RestPage;
import com.drama.api.sys.video.domain.vo.VideoTaskSearchVO;
import com.drama.api.sys.video.domain.vo.VideoTaskVO;
import com.drama.api.sys.video.service.IVideoTaskService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;


@Api( tags = ApiIndex.SysTranscodingController)
@RequestMapping(value = "/api/sys/video")
@RestController
@Slf4j
public class VideoTaskController {

    @Autowired
    private IVideoTaskService service;

    @ApiOperation(value = "查询转码任务列表")
    @GetMapping(value = "/list")
    @SaCheckLogin
    public R<RestPage<List<VideoTaskVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute VideoTaskSearchVO searchVO) {
        List<VideoTaskVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage(new PageInfo(list), list);
        return new R(restPage);
    }



    @ApiOperation(value = "添加转码任务")
    @PostMapping(value = "/add")
    @SaCheckLogin
    public R add( @ApiParam(value = "视频ID") @Valid @RequestBody IDEntity id) {
        service.add(id.getId());
        return new R();
    }


    @ApiOperation(value = "重启转码任务")
    @PostMapping(value = "/reset")
    @SaCheckLogin
    public R reset( @ApiParam(value = "任务ID") @Valid @RequestBody IDEntity id) {
        service.reset(id.getId());
        return new R();
    }

    @ApiOperation(value = "转码成功通知")
    @PostMapping(value = "/action")
    public R action(HttpServletRequest request) throws IOException {
        log.info("华为云信息通知：：：：：：：：：：：：：：：：：");
        // 1.从请求体中读取【输入字节流】
        ServletInputStream inputStream = request.getInputStream();

        // 2.将字节流转换为【字符流】
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);

        // 3.将字符流转为【缓冲字符流】
        BufferedReader streamReader = new BufferedReader(inputStreamReader);

        // 4.逐行读取
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = streamReader.readLine()) != null) {
            // 5.重新拼接每一行，还原字符串
            sb.append(line);
        }
        log.info(sb.toString());


        JSONObject jsonObject=new JSONObject(sb.toString());
        if(jsonObject.containsKey("message")&&jsonObject.getJSONObject("message").containsKey("transcode_info")){
            log.info("华为云成功转码信息通知：：：：：：：：：：：：：：：：：");
//            log.info(jsonObject.getJSONObject("message").toString());
//            log.info(jsonObject.getJSONObject("message").getJSONObject("transcode_info").toString());
//            log.info(jsonObject.getJSONObject("message").getJSONObject("transcode_info").getStr("task_id"));
//            log.info(jsonObject.getJSONObject("message").getJSONObject("transcode_info").getStr("status"));
//            log.info(jsonObject.getJSONObject("message").getJSONObject("transcode_info").getJSONObject("output").getStr("object"));
//            log.info(jsonObject.getJSONObject("message").getJSONObject("transcode_info").getJSONObject("media_detail").getJSONArray("output_video_paras").getJSONObject(0).getStr("file_name"));
//            log.info(jsonObject.getJSONObject("message").getJSONObject("transcode_info").getJSONObject("media_detail").getJSONArray("output_video_paras").getJSONObject(0).getStr("size"));
            String taskId=jsonObject.getJSONObject("message").getJSONObject("transcode_info").getStr("task_id");
            String status=jsonObject.getJSONObject("message").getJSONObject("transcode_info").getStr("status");
            if(status.equals("SUCCEEDED")){
                String outPath=jsonObject.getJSONObject("message").getJSONObject("transcode_info").getJSONObject("output").getStr("object");
                String outFileName=jsonObject.getJSONObject("message").getJSONObject("transcode_info").getJSONObject("media_detail").getJSONArray("output_video_paras").getJSONObject(0).getStr("file_name");
                String fileSize=jsonObject.getJSONObject("message").getJSONObject("transcode_info").getJSONObject("media_detail").getJSONArray("output_video_paras").getJSONObject(0).getStr("size");
                String rate=jsonObject.getJSONObject("message").getJSONObject("transcode_info").getJSONObject("media_detail").getJSONArray("output_video_paras").getJSONObject(0).getJSONObject("video").getStr("bitrate");
                service.opAction(taskId,status,outPath,outFileName,fileSize,rate);
            }else {
                service.opAction(taskId,status,"","","","");
            }

        }


        return new R();
    }



}